home *** CD-ROM | disk | FTP | other *** search
/ CD ROM Paradise Collection 4 / CD ROM Paradise Collection 4 1995 Nov.iso / program / swagn_r.zip / PRINTING.SWG / 0005_LP.PAS.pas < prev    next >
Pascal/Delphi Source File  |  1993-05-28  |  4KB  |  107 lines

  1. { The following Program, LPRINT, illustrates how to do control a    }
  2. { Printer directly without using the BIOS (Printers connected to    }
  3. { the parallel port, not serial Printers connected to an RS-232     }
  4. { port).                                                            }
  5. { LPRINT checks to see if you want to print a line from the command }
  6. { prompt, as in:                                                    }
  7. {        LPRINT Hello, World!                                       }
  8. { If there's no command input, LPRINT checks For Characters at the  }
  9. { "standard input," so you can print Files or directories using     }
  10. { redirection or piping:    LPRINT < myFile.pas                     }
  11. {                           DIR | LPRINT                            }
  12. { LPT1 is used. You can modify LPRINT to use another, or be able to }
  13. { specify which Printer via the command line (eg. /2 For LPT2,etc.) }
  14. { This source code is a bit cramped, to fit into one message.       }
  15. {                                                                   }
  16.  
  17. Program LPRINT;
  18. Uses
  19.   Dos;
  20. Const
  21.   BusyB   =$80;                   { status port 'busy' bit    }
  22.   AckB    =$40;                   { status port 'ack' bit     }
  23. Var
  24.   DataP,
  25.   Strobe,
  26.   Status,                         { assigned lpt i/o ports    }
  27.   MaxWait : Word;                 { seconds before timing out }
  28.   Done    : Boolean;              { sanity clause             }
  29.   Reg     : Registers;            { For Dos i/o               }
  30.   txtptr  : Byte;                 { counter Byte              }
  31.  
  32. Procedure VerifyPrinter( Var Printer, Status, Strobe : Word );
  33. { check For presence of specified Printer - returning ports         }
  34. begin
  35.   if Printer in [1..3] then         { must be known     }
  36.   begin
  37.     DEC( Printer );                 { For 0..2          }
  38.     Printer := MemW[$40 : (Printer + 8 + Printer * 2)];
  39.     if ((Port[Printer + 1] and AckB) = 0) then
  40.       Printer := 0           { to say it's not there }
  41.     else
  42.     begin
  43.       Status := Printer + 1;
  44.       Strobe := Printer + 2;
  45.     end
  46.   end
  47. end; {VerifyPrinter}
  48.  
  49. Procedure Print( DataP : Word; chout : Byte; Var Done : Boolean);
  50. { send Character to Printer port, With busy timeout and feedback    }
  51. Var
  52.   WaitTime : LongInt;
  53.   Timer    : LongInt Absolute 0:$046c;
  54.   BusyWait : Word;
  55. begin
  56.   BusyWait := 0;
  57.   WaitTime := Timer;
  58.   While ((Port[Status] and BusyB) = 0) and (BusyWait < MaxWait * 19) do
  59.   { wait up to MaxWait seconds For non-busy state             }
  60.     BusyWait := Word( Timer - WaitTime );
  61.   if BusyWait >= (MaxWait * 19) then { Printer "busy" For too long? }
  62.     Done := False              { failed            }
  63.   else
  64.   begin
  65.     Port[DataP]  := chout;     { send the Char data}
  66.     Port[Strobe] := $0c;       { strobe it in      }
  67.     Port[Strobe] := $0d;       { reset strobe      }
  68.     Done := True;              { success           }
  69.   end {else}
  70. end; {Print}
  71.  
  72. begin   {LPRINT}
  73.   WriteLn(#10, 'LPRINT v1.0 G.S.Vigneault', #10);
  74.   DataP := 1;     { LPT1 }
  75.   VerifyPrinter( DataP, Status, Strobe );
  76.   { DataP will be 0 now if requested Printer didn't respond   }
  77.   if DataP = 0 then
  78.   begin
  79.     WriteLn('Printer not detected!',#10,#7);
  80.     Halt(1);
  81.   end;
  82.   MaxWait := 10;  { max wait 10sec before timing out lpt      }
  83.   if ParamCount = 0 then  { no command-line input?            }
  84.   { handle redirected and piped }
  85.   Repeat
  86.     Reg.AH := $b;   { to see if a Char is available     }
  87.     MsDos( Reg );
  88.     if Reg.AL <> 0 then
  89.     begin
  90.       Reg.AH := 8;            { get the Char      }
  91.       MsDos( Reg );           { via Dos           }
  92.       Print( DataP, Reg.AL, Done );{ lprint it    }
  93.     end; {if}
  94.   Until (Reg.AL = 0) or not Done
  95.   else    { print the command line Text }
  96.   begin
  97.     txtptr := $82;
  98.     Repeat
  99.       Print( DataP, Mem[PrefixSeg:txtptr], Done );
  100.       INC( txtptr );
  101.     Until (Mem[PrefixSeg:txtptr] = 13) or not Done;
  102.   if Done then
  103.     Print( DataP, 10, Done);       { lf    }
  104.   end;
  105. end {LPRINT}.
  106. (********************************************************************)
  107.